﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using FrameWork;
using FrameWork.Components;
using FrameWork.WebControls;


namespace FrameWork.web.Module.ITMS.Upload.UploadFile
{
    public partial class _default : System.Web.UI.Page
    {
        private string cmd = (string)Common.sink("CMD", MethodType.Get, 255, 0, DataType.Str);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BusinessFacade.CreateDir(Server.MapPath(Common.UpLoadDir + FileUploadPath));
                DataBindList();
                if (cmd != "")
                {
                    GridView1.Columns[4].Visible = false;
                }
            }
            if (cmd == "Delete")
            {
                FrameWorkPermission.CheckPermissionVoid(PopedomType.Delete);
                string FileName = (string)Common.sink("FileName", MethodType.Get, 255, 1, DataType.Str);
                FileUpLoadCommon.DeleteFile(string.Format("{0}{1}{2}", Common.UpLoadDir, FileUploadPath + "/", FileName));
                EventMessage.MessageBox(1, "操作成功", string.Format("删除文件{0}成功!", FileName), Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            DataBindList();
        }

        [PopedomTypeAttaible(PopedomType.New)]
        [System.Security.Permissions.PrincipalPermission(System.Security.Permissions.SecurityAction.Demand, Role = "OR")]
        protected void Button1_Click(object sender, EventArgs e)
        {
            FileUpLoadCommon fc = new FileUpLoadCommon(Common.UpLoadDir + FileUploadPath + "/", false);
            if (!fc.SaveFile(FileUpload1.PostedFile, false, true))
                EventMessage.MessageBox(1, "上传失败!", fc.errorMsg, Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx?CMD=" + cmd));
            else
                EventMessage.MessageBox(1, "操作成功", "上传成功!", Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx?CMD=" + cmd));
            fc = null;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void DataBindList()
        {
            List<FileEntity> lst = GetFiles(Server.MapPath(Common.UpLoadDir + "/" + FileUploadPath + "/"));
            lst.Sort();

            AspNetPager1.RecordCount = lst.Count;

            ArrayList lists = new ArrayList();
            for (int i = AspNetPager1.StartRecordIndex; i <= AspNetPager1.EndRecordIndex; i++)
            {
                lists.Add(lst[i - 1]);
            }


            GridView1.DataSource = lists;
            GridView1.DataBind();
        }

        private List<FileEntity> GetFiles(string FileDir)
        {
            List<FileEntity> FileList = new List<FileEntity>();
            DirectoryInfo dirInfo = new DirectoryInfo(FileDir);
            foreach (FileInfo var in dirInfo.GetFiles())
            {
                FileEntity fe = new FileEntity();
                fe.FileCreateDate = var.LastWriteTime;
                fe.FileName = var.Name;
                fe.FileSize = var.Length / 1024;
                fe.FileType = var.Extension;
                FileList.Add(fe);
            }
            return FileList;
        }


        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && cmd == "Select")
            {
                FileEntity Fe = (FileEntity)e.Row.DataItem;
                if (Fe != null)
                {
                    e.Row.Attributes.Add("onmouseover", "this.style.cursor='pointer';");
                    string filename = Common.UpLoadDir + FileUploadPath + "/" + Fe.FileName;
                    e.Row.Attributes.Add("OnClick", "top.document.forms[0].elements['ImgUrl'].value='" + filename + "';top.document.PREVIEWPIC.src='" + filename + "';");
                }
            }
        }

        private string FileUploadPath
        {
            get
            {
                return ConfigurationManager.AppSettings["FileUploadPath"] + "";
            }
        }
    }

    /// <summary>
    /// 文件实体类
    /// </summary>
    public class FileEntity : IComparable
    {

        #region "Private Variables"
        private string _FileName;
        private string _FileType;
        private long _FileSize;
        private DateTime _FileCreateDate;
        #endregion

        #region "Public Variables"
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get
            {
                return _FileName;
            }
            set
            {
                _FileName = value;
            }
        }
        /// <summary>
        /// 文件类型
        /// </summary>
        public string FileType
        {
            get
            {
                return _FileType;
            }
            set
            {
                _FileType = value;
            }
        }

        /// <summary>
        /// 文件大小(k)
        /// </summary>
        public long FileSize
        {
            get
            {

                return _FileSize;
            }
            set
            {
                _FileSize = value;
            }
        }

        /// <summary>
        /// 文件创建日期
        /// </summary>
        public DateTime FileCreateDate
        {
            get
            {
                return _FileCreateDate;
            }

            set
            {
                _FileCreateDate = value;
            }
        }
        #endregion

        /// <summary>
        /// 重写比较类
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int CompareTo(object o)
        {
            FileEntity Two = o as FileEntity;
            if (Two.FileCreateDate > FileCreateDate)
                return 1;
            else if (Two.FileCreateDate < FileCreateDate)
                return -1;
            else
                return 0;
        }


    }


}
